<?php
/**
 * md6加密
 * @author  jackwoo
 */
if (!function_exists('md6')) {
    function md6($cont)
    {
        return md5($cont . 'dubooker' . $cont);
    }
}

/**
 * 校验邮箱
 * @author  jackwoo
 */
if (!function_exists('isemail')) {
    function isemail($email)
    {
        return strlen($email) > 6 && strlen($email) <= 32 && preg_match("/^([A-Za-z0-9\-_.+]+)@([A-Za-z0-9\-]+[.][A-Za-z0-9\-.]+)$/", $email);
    }
}
/**
 * 检查是否是移动设备
 * @author  jackwoo
 */
if (!function_exists('is_mobile')) {
    function is_mobile()
    {
        $CI =& get_instance();
        $user_agent = $CI->input->server('HTTP_USER_AGENT');
        $mobile_agents = Array("240x320", "acer", "acoon", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly_", "fly-", "go.web", "goodaccess", "gradiente", "grundig", "haier", "hedy", "hitachi", "htc", "huawei", "hutchison", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up.browser", "utec", "utstar", "verykool", "virgin", "vk-", "voda", "voxtel", "vx", "wap", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte");
        $is_mobile = false;
        foreach ($mobile_agents as $device) {
            if (stristr($user_agent, $device)) {
                $is_mobile = true;
                break;
            }
        }
        return $is_mobile;
    }
}
/**
 * 检测浏览器版本
 * @author jackwoo
 */
if (!function_exists('check_browser')) {
    function check_browser()
    {
        $CI =& get_instance();
        $CI->load->library('user_agent');
        if ($CI->agent->is_browser()) {
            $browser = $CI->agent->browser();
            $version = $CI->agent->version();

            if ($browser == "Internet Explorer" && intval($version) < 8) {
                return FALSE;
            } else {
                return TRUE;
            }
        } elseif ($CI->agent->is_robot()) {
            return TRUE;
        } elseif ($CI->agent->is_mobile()) {
            return TRUE;
        } else {
            return TRUE;
        }
    }
}
/**
 * 截取字符串
 * @author jackwoo
 */
if (!function_exists('cutstr')) {
    function cutstr($string, $len, $ellipsis = "...", $encoding = 'utf8')
    {
        if (mb_strlen($string, $encoding) <= $len) {
            return $string;
        } else {
            return mb_substr($string, 0, $len, $encoding) . $ellipsis;
        }
    }
}
/**
 * 检查URL的合法性，检测URL头是否为 http, https, ftp
 * @param string $url url
 * @return boolean
 */
if (!function_exists('check_url')) {
    function check_url($url)
    {
        $allow = array("http:\/\/", "https:\/\/", "ftp:\/\/");
        if (preg_match("/^(" . implode("|", $allow) . ").+\..+$/i", $url)) {
            return true;
        }
        return false;
    }
}
if (!function_exists('infiniteAssort')) {
    /**
     * 对二维数组进行无限极分类函数(一定要存在父子关系)
     * 调用实例
     * $data=array(
     *      array('id'=>1,name=>'分类1','parent'=>0),
     *      array('id'=>2,name=>'分类2','parent'=>0),
     *      array('id'=>3,name=>'分类3','parent'=>0),
     *      array('id'=>4,name=>'分类4','parent'=>1),
     *      array('id'=>5,name=>'分类5','parent'=>1),
     *      array('id'=>6,name=>'分类6','parent'=>2),
     *      array('id'=>7,name=>'分类7','parent'=>3),
     *      array('id'=>8,name=>'分类8','parent'=>7)
     * );
     * infiniteAssort($data,0,0,"parent","id",$result,"p_level");
     * print_r($result);
     *
     * @param array $array 二维数组
     * @param int $parent 父类ID
     * @param int $level 层次号
     * @param string $parentKeyName 父类键名
     * @param string $sonKeyName 子类键名
     * @param mixed &$result 保存最终结果
     */
    function  infiniteAssort($array, $parent, $level, $parentKeyName, $sonKeyName, &$result, $levelName = "level", $mode = 1, $fields = "")
    {
        if ($mode == 1) {
            for ($i = 0; $i < count($array); $i++) {
                if ($array[$i][$parentKeyName] == $parent) {
                    $array[$i][$levelName] = $level;
                    $sub[] = $array[$i];
                }
            }
            if (isset($sub) && count($sub) > 0) {
                for ($j = 0; $j < count($sub); $j++) {
                    $result[] = $sub[$j];
                    infiniteAssort($array, $sub[$j][$sonKeyName], $level + 1, $parentKeyName, $sonKeyName, $result, $levelName, $mode, $fields);
                }
            }
        } else if ($mode == 2) {
            for ($i = 0; $i < count($array); $i++) {
                if ($array[$i][$parentKeyName] == $parent) {
                    $array[$i][$levelName] = $level;
                    $sub[] = $array[$i];
                }
            }
            if (isset($sub) && count($sub) > 0) {
                for ($j = 0; $j < count($sub); $j++) {
                    if (!empty($fields)) {
                        foreach ($fields as $k => $v) {
                            $temp[$v] = $sub[$j][$k];
                        }
                        $result["children"][] = $temp;
                    } else {
                        $result["children"][] = $sub[$j];
                    }

                    infiniteAssort($array, $sub[$j][$sonKeyName], $level + 1, $parentKeyName, $sonKeyName, $result["children"], $levelName, $mode, $fields);
                }
            }
        }
    }
}
//获得文件扩展
if (!function_exists("get_file_suffix")) {
    function get_file_suffix($file)
    {
        $suffix = strtolower(substr($file, (strrpos($file, ".", 1) + 1)));
        return $suffix;
    }
}


//管理员后台消息页面
if (!function_exists('show_msg')) {
    function show_msg($message = '', $template = 'message')
    {
        include(VIEWPATH . $template . '.php');
        exit();
    }
}

//检测登陆状态
if (!function_exists('check_privilege')) {
    function  check_privilege()
    {
        $is_login = FALSE;
        $admin_username = session('admin_username');
        if (!empty($admin_username)) {
            $is_login = TRUE;
        }
        if (empty($is_login)) {
            redirect('/Clover/login');
        }
    }
}

if (!function_exists('add_log')) {
    function add_log()
    {

    }
}

/**
 * 通过IP获取对应城市信息(该功能基于淘宝第三方IP库接口)
 * @param $ip IP地址,如果不填写，则为当前客户端IP
 * @return  如果成功，则返回数组信息，否则返回false
 */
if (!function_exists('getIpInfo')) {
    function getIpInfo($ip)
    {
        if (empty($ip)) $ip = get_client_ip();  //get_client_ip()为tp自带函数，如没有，自己百度搜索。此处就不重复复制了
        $url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
        $result = file_get_contents($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 0 || !is_array($result['data'])) return false;
        return $result['data'];
    }
}
/* End of file common_helper.php */
/* Location: ./system/helpers/common_helper.php */